<template>
  <div class="dashboard-container">
    <div class="app-container">

      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="privileges(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="compile(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="removeList(row.id)">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination layout="prev,pager,next" :total="total" :page-size="page.pagesize" @current-change="currentchange" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.companyname" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.location" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailboxs" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
      <el-dialog v-if="showDialog" title="编辑弹层" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>

      </el-dialog>
      <!-- 分配权限弹窗 -->
      <el-dialog title="分配权限" :visible="showlog" @close="btnPermCancel">
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <el-tree
          ref="permTree"
          :data="permData"
          :props="{label:'name'}"
          show-checkbox
          check-strictly
          default-expand-all
          node-key="id"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  getCompanyInfo,
  getRoleList,
  removeRoleList,
  addRoleList,
  getroleInfo,
  setroleInfo
} from '@/api/setting'
import { getPermissionList, rolebasedAccessControl } from '@/api/permission'
import { statisticalData } from '@/utils'
export default {

  data() {
    return {
      permData: [],
      showlog: false,
      showDialog: false,
      companyInfo: {},
      list: [],
      page: {
        page: 1, // 页
        pagesize: 2 // 每页的数据条数
        // 数据总条数
      },
      total: 0,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { max: 12, message: '长度在 12个字符之内', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCompanyInfo()
    this.getRoleList()
    this.PowerData()// 加载所有的权限
  },
  methods: {
    async btnPermOK() {
      await rolebasedAccessControl({
        permIds: this.$refs.permTree.getCheckedKeys(),
        id: this.roleId
      })
      this.$message.success('添加成功')
      this.showlog = false
    },
    async PowerData() {
      this.permData = statisticalData(await getPermissionList(), '0')
    },
    async privileges(id) { // 分配权限  打开模态框
      const { permIds } = await getroleInfo(id)
      this.roleId = id
      this.showlog = true
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    async getCompanyInfo() { // 获取数据，改造数据
      const res = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
      this.companyInfo = { ...res, companyname: ' 北京田米科技股份有限公司 ',
        location: '北京市海淀区西二旗中路33号院6号楼3层006号',
        mailboxs: 'chenchongwei@xiaomi.com',
        remarks: '市场主体依法自主选择经营项目，开展经营活动；依法须经批准的项目，经相关部门批准后依批准的内容开展经营活动' }
    },
    async getRoleList() { // 获取分页信息，获取角色总条数
      const res = await getRoleList(this.page)
      this.total = res.total
      this.list = res.rows

      // console.log(res)
    },
    currentchange(newpage) { // 切换分页重新发请求，渲染页面
      this.page.page = newpage
      // 重新渲染数据
      this.getRoleList()
    },
    async removeList(id) { // 删除角色
      // 提醒用户
      await this.$confirm('是否确认删除')
      // 删除
      await removeRoleList(id)
      this.$message.success('删除成功')
      if (this.list.length === 1 && this.page.page !== 1) {
        this.page.page -= 1
        // this.page.page = this.page.page - 1
        this.getRoleList()
      }
      // 重新渲染数据
      this.getRoleList()
    },
    addRole() { // 新增角色打开模态框
      this.showDialog = true
    },
    async  btnCancel() { // 编辑或新增弹窗取消按钮
      this.roleForm = {
        name: '',
        description: ''
      }
      await this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    btnPermCancel() { // 权限弹窗权限按钮
      this.showlog = false
      this.$refs.permTree.setCheckedKeys([])// 清空数据
    },
    async btnOK() { // 确认修改或编辑  调用接口发请求
      if (this.roleForm.id) {
        // 编辑
        setroleInfo(this.roleForm)
        this.$message.success('编辑成功')
      } else {
        // 添加
        await this.$refs.roleForm.validate()
        await addRoleList(this.roleForm)
        this.$message.success('新增成功')
      }
      this.showDialog = false
      this.getRoleList()
    },
    async  compile(id) { // 编辑数据回显
      this.showDialog = true
      this.roleForm = await getroleInfo(id)
    }
  }

}
</script>

<style scoped lang="less">

</style>
